
//% color="#ef784c" iconWidth=50 iconHeight=40
namespace djiSingleTello{

    
    //% block="djitello begin " blockType="command"


    export function djitellopy_Iint(parameter: any, block: any) {

        
        Generator.addImport(`from djitellopy import Tello\nimport cv2\nfrom djitellopy import TelloSwarm`)
        Generator.addImport(`import requests\nfrom PIL import Image\nimport io`)
    
    }

    
    //% block="djitello_connect_wifi [wifiname] [wifipassword]" blockType="command"
    //% wifiname.shadow="string" wifiname.defl=HONOR-ROUTER
    //% wifipassword.shadow="string" wifipassword.defl=902902902

    export function djitello_connect_wifi(parameter: any, block: any) {
        let wifiname=parameter.wifiname.code;
        let wifipassword=parameter.wifipassword.code;

        Generator.addCode(`tello = Tello()\ntello.connect()\ntello.connect_to_wifi(ssid=${wifiname},password=${wifipassword})`)
  
    }

    //% block="djitello_connect [ipdress]" blockType="command"
    //% ipdress.shadow="string" ipdress.defl=192.168.10.1

    export function djitello_connect(parameter: any, block: any) {
        let ipdress=parameter.ipdress.code;

        Generator.addCode(`tello = Tello(host=${ipdress})\ntello.connect()`)
        
    } 


    //% block="djitello_streamon " blockType="command"

    export function djitello_streamon(parameter: any, block: any) {

        Generator.addCode(`tello.streamon()`)

    } 


    //% block="djitello_streamoff " blockType="command"

    export function djitello_streamoff(parameter: any, block: any) {

        Generator.addCode(`tello.streamoff()`)

    } 

    
    //% block="djitello_picture_upload [url]" blockType="command"
    //% url.shadow="string"  url.defl="http://localhost:5000/"

    export function djitello_picture_upload(parameter: any, block: any) {
        let url=parameter.url.code;

        Generator.addCode(`frame_read = tello.get_frame_read()\nmyimg=frame_read.frame\nimage = Image.fromarray(myimg.astype('uint8'))`)
        Generator.addCode(`image_bytes = io.BytesIO()\nimage.save(image_bytes, format='png')\nfiles = {'file': ('myimg.jpg',image_bytes.getvalue())}`)
        Generator.addCode(`response = requests.post(${url}, files=files)\nprint(response)`)

    } 


    //% block="Led_color[LEDCOLOR]" blockType="command"
    //% LEDCOLOR.shadow="dropdown" LEDCOLOR.options="LEDCOLOR"
    
    export function Led_color(parameter: any, block: any) {
        let ledcolor=parameter.LEDCOLOR.code;
 
        Generator.addCode(`tello.send_expansion_command("${ledcolor}")`)

    }

    //% block="RGB_Ledcolor[R] [G] [B]" blockType="command"
    //% R.shadow="number"  R.defl="255"
    //% G.shadow="number"  G.defl="255"
    //% B.shadow="number"  B.defl="255"
    
    export function RGB_Ledcolor(parameter: any, block: any) {
        let R=parameter.R.code;
        let G=parameter.G.code;
        let B=parameter.B.code;
 
        Generator.addCode(`tello.send_expansion_command("led ${R} ${G} ${B}")`)

    }

    //% block="RGB_breath[Rb] [Gb] [Bb] [t]" blockType="command"
    //% Rb.shadow="number"  Rb.defl="255"
    //% Gb.shadow="number"  Gb.defl="255"
    //% Bb.shadow="number"  Bb.defl="255"
    //% t.shadow="number"  t.defl="0.1"
    
    export function RGB_breath(parameter: any, block: any) {
        let Rb=parameter.Rb.code;
        let Gb=parameter.Gb.code;
        let Bb=parameter.Bb.code;
        let t=parameter.t.code;
 
        Generator.addCode(`tello.send_expansion_command("led br ${t} ${Rb} ${Gb} ${Bb}")`)

    }

    //% block="RGB_flicker [t_flicker] [R1] [G1] [B1] [R2] [G2] [B2]" blockType="command"
    //% t_flicker.shadow="number"  t_flicker.defl="0.1"
    //% R1.shadow="number"  R1.defl="255"
    //% G1.shadow="number"  G1.defl="255"
    //% B1.shadow="number"  B1.defl="255"
    //% R2.shadow="number"  R2.defl="255"
    //% G2.shadow="number"  G2.defl="255"
    //% B2.shadow="number"  B2.defl="255"
    
    export function RGB_flicker(parameter: any, block: any) {
        let t_flicker=parameter.t_flicker.code;
        let R1=parameter.R1.code;
        let G1=parameter.G1.code;
        let B1=parameter.B1.code;
        let R2=parameter.R2.code;
        let G2=parameter.G2.code;
        let B2=parameter.B2.code;
 
        Generator.addCode(`tello.send_expansion_command("led bl ${t_flicker} ${R1} ${G1} ${B1} ${R2} ${G2} ${B2}")`)

    }

    //% block="Led_color[MLEDCOLOR] [MLEDSTRING]" blockType="command"
    //% MLEDCOLOR.shadow="dropdown" MLEDCOLOR.options="MLEDCOLOR"
    //% MLEDSTRING.shadow="dropdown" MLEDSTRING.options="MLEDSTRING"
    
    export function MLed_color(parameter: any, block: any) {
        let mledcolor=parameter.MLEDCOLOR.code;
        let mledstring=parameter.MLEDSTRING.code;
 
        Generator.addCode(`tello.send_expansion_command("mled s ${mledcolor} ${mledstring}")`)

    }

    //% block="djitello_sn" blockType="command"

    export function djitello_sn(parameter: any, block: any) {

        Generator.addCode(`print(tello.query_serial_number())`)
        
    }

    //% block="djitello_battery " blockType="command"

    export function djitello_battery(parameter: any, block: any) {

        Generator.addCode(`print(tello.query_battery())`)
        
    }

    //% block="djitello_motoron " blockType="command"

    export function djitello_motoron(parameter: any, block: any) {

        Generator.addCode(`tello.turn_motor_on()`)
        
    }

    //% block="djitello_motoroff " blockType="command"

    export function djitello_motoroff(parameter: any, block: any) {

        Generator.addCode(`tello.turn_motor_off()`)
        
    }

    //% block="djitello_takeoff " blockType="command"

    export function djitello_takeoff(parameter: any, block: any) {

        Generator.addCode(`tello.takeoff()`)
        
    }

    //% block="djitello_land " blockType="command"

    export function djitello_land(parameter: any, block: any) {

        Generator.addCode(`tello.land()`)
        
    }

    //% block="djitello_moveup [up]" blockType="command"
    //% up.shadow="number"  up.defl="20"

    export function djitello_moveup(parameter: any, block: any) {
        let up=parameter.up.code;

        Generator.addCode(`tello.move_up(${up})`)
        
    }

    //% block="djitello_movedown [down]" blockType="command"
    //% down.shadow="number"  down.defl="20"

    export function djitello_movedown(parameter: any, block: any) {
        let down=parameter.down.code;

        Generator.addCode(`tello.move_down(${down})`)
        
    }

    //% block="djitello_moveforward [forward]" blockType="command"
    //% forward.shadow="number"  forward.defl="20"

    export function djitello_moveforward(parameter: any, block: any) {
        let forward=parameter.forward.code;

        Generator.addCode(`tello.move_forward(${forward})`)
        
    }

    //% block="djitello_moveback [back]" blockType="command"
    //% back.shadow="number"  back.defl="20"

    export function djitello_moveback(parameter: any, block: any) {
        let back=parameter.back.code;

        Generator.addCode(`tello.move_back(${back})`)
        
    }

    //% block="djitello_moveright [right]" blockType="command"
    //% right.shadow="number"  right.defl="20"

    export function djitello_moveright(parameter: any, block: any) {
        let right=parameter.right.code;

        Generator.addCode(`tello.move_right(${right})`)
        
    }

    //% block="djitello_moveleft [left]" blockType="command"
    //% left.shadow="number"  left.defl="20"

    export function djitello_moveleft(parameter: any, block: any) {
        let left=parameter.left.code;

        Generator.addCode(`tello.move_left(${left})`)
        
    }

    //% block="djitello_Srotate [srotate]" blockType="command"
    //% srotate.shadow="number"  srotate.defl="5"

    export function djitello_Srotate(parameter: any, block: any) {
        let srotate=parameter.srotate.code;

        Generator.addCode(`tello.rotate_clockwise(${srotate})`)
        
    }

    //% block="djitello_Nrotate [nrotate]" blockType="command"
    //% nrotate.shadow="number"  nrotate.defl="5"

    export function djitello_Nrotate(parameter: any, block: any) {
        let nrotate=parameter.nrotate.code;

        Generator.addCode(`tello.rotate_counter_clockwise(${nrotate})`)
        
    }

    //% block="djitello_challenge " blockType="command"

    export function djitello_challenge(parameter: any, block: any) {

        Generator.addCode(`tello.enable_mission_pads()\ntello.set_mission_pad_detection_direction(0)`)
        
    }

    //% block="djitello_challengefly [x] [y] [z] [speed] [mid]" blockType="command"
    //% x.shadow="number"  x.defl="0"
    //% y.shadow="number"  y.defl="0"
    //% z.shadow="number"  z.defl="0"
    //% speed.shadow="number"  speed.defl="10"
    //% mid.shadow="number"  mid.defl="1"

    export function djitello_challengefly(parameter: any, block: any) {
        let x=parameter.x.code;
        let y=parameter.y.code;
        let z=parameter.z.code;
        let speed=parameter.speed.code;
        let mid=parameter.mid.code;

        Generator.addCode(`tello.go_xyz_speed_mid(${x},${y},${z},${speed},${mid})`)
        
    }

}
